package com.LiL.Wayne.Activity.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.SlidingMenu.ActivityBase;
import com.LiL.Wayne.Activity.SlidingMenu.SlidingMenuBuilderConcrete;
import com.LiL.Wayne.Activity.activity.MusicPlayer;
import com.audamob.audasingers.model.Music;
import com.audamob.audasingers.tool.adapter.SwipeyTabsPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

public class MainContainerActivity extends ActivityBase {

	private static final String[] TITLES = { "Settings", "Videos", "Favorites",
			"Music", "Playlist", "Lyrics", "News", "Tweets", "Bio" };

	public static String PlaylistName = "";
	private TabPageIndicator mTabs;
	private static ViewPager mViewPager;
	public static MainContainerActivity activity;

	// Your need to put this method in every Activity class where you want to
	// have sliding menu.
	@Override
	public Class<?> setSlidingMenu() {
		// Each activity can have it's own sliding menu controlling builder
		// class.
		return SlidingMenuBuilderConcrete.class;
	}

	@Override
	public boolean enableHomeIconActionSlidingMenu() {
		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		activity = this;
		setContentView(R.layout.auda_layout_maincontainer);

		edit_done = (RelativeLayout) findViewById(R.id.layout_edit);
		edit_done.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Done_edit_case();
			}

		});
		request = (RelativeLayout) findViewById(R.id.layout_request);
		request.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Request_case();
			}

		});
		edit_List = (RelativeLayout) findViewById(R.id.layout_edit_edit);
		edit_List.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Edit_List();
			}

		});

		ChangeCurrentFragment(1);

		//mViewPager.setOnPageChangeListener(mTabs);
		//mViewPager.setCurrentItem(1);

	}

	public static void ChangeCurrentFragment(int i) {
		ViewPager pager = (ViewPager) activity.findViewById(R.id.viewpager);
		FragmentManager fragmentManager;
		fragmentManager = activity.getSupportFragmentManager();

		try {
			pager.removeAllViews();
		} catch (Exception e) {
		}

		String[] TITLES = CreateTableString(i);
		SwipeyTabsPagerAdapter adapter = new SwipeyTabsPagerAdapter(activity,
				fragmentManager, TITLES, pager, i);

		pager.setAdapter(adapter);

		TabPageIndicator indicator = (TabPageIndicator) activity
				.findViewById(R.id.indicator);
		indicator.setViewPager(pager);

	}
	public static String[] CreateTableString(int i) {
		switch (i) {
		case 1:
			String[] tab_1 = { activity.getResources().getString(
					R.string.Done_text) };
			return tab_1;
			
		
	default:
		String[] tab_2 = { activity.getResources().getString(
				R.string.Done_text) };
		return tab_2;
		}
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();

	}

	public static float Dip_To_Px(int val) {

		Resources r = activity.getResources();
		float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, val,
				r.getDisplayMetrics());
		return px;
	}

	public static RelativeLayout edit_done;
	public static RelativeLayout edit_List;
	public static RelativeLayout request;

	public static void Done_edit_case() {
		// TODO Auto-generated method stub
		int current_tab = mViewPager.getCurrentItem();
		switch (current_tab) {
		case 1:
			//SwipeyTabFragmentVideos.Mode_Edit_Of();
			edit_done.setVisibility(View.INVISIBLE);
			MainContainerActivity.request.setVisibility(View.VISIBLE);
			mViewPager.setCurrentItem(2);
			break;
		case 2:
			//SwipeyTabFragmentFavorites.Mode_Edit_of();
			edit_done.setVisibility(View.INVISIBLE);
			MainContainerActivity.edit_List.setVisibility(View.VISIBLE);

			break;
		case 3:
			SwipeyTabFragmentMusic.Mode_Edit_Of();
			edit_done.setVisibility(View.INVISIBLE);
			MainContainerActivity.request.setVisibility(View.VISIBLE);
			mViewPager.setCurrentItem(4);
			break;
		case 4:
			SwipeyTabFragmentMusicPlayList.Mode_Edit_of();
			edit_done.setVisibility(View.INVISIBLE);
			MainContainerActivity.edit_List.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
	}

	public static void swipe_notdone() {
		try {

			edit_done.setVisibility(View.INVISIBLE);
			MainContainerActivity.request.setVisibility(View.VISIBLE);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void Request_case() {
		// TODO Auto-generated method stub
		int current_tab = mViewPager.getCurrentItem();
		switch (current_tab) {
		case 1:
			final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT,
					"Lil Wayne Application : Add Video");
			String[] tos = { "theateamdeveloper@gmail.com" };
			intent.putExtra(Intent.EXTRA_TEXT, "Video Name :");
			intent.putExtra(Intent.EXTRA_EMAIL, tos);
			final PackageManager pm = getPackageManager();
			final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
					0);
			ResolveInfo best = null;
			for (final ResolveInfo info : matches)
				if (info.activityInfo.packageName.endsWith(".gm")
						|| info.activityInfo.name.toLowerCase().contains(
								"gmail"))
					best = info;
			if (best != null)
				intent.setClassName(best.activityInfo.packageName,
						best.activityInfo.name);
			startActivity(intent);
			break;
		case 3:
			final Intent intent2 = new Intent(
					android.content.Intent.ACTION_SEND);
			intent2.setType("text/plain");
			intent2.putExtra(Intent.EXTRA_SUBJECT,
					"Lil Wayne Application : Add Song");
			String[] tos2 = { "theateamdeveloper@gmail.com" };
			intent2.putExtra(Intent.EXTRA_TEXT, "Song Name :");
			intent2.putExtra(Intent.EXTRA_EMAIL, tos2);
			final PackageManager pm2 = getPackageManager();
			final List<ResolveInfo> matches2 = pm2.queryIntentActivities(
					intent2, 0);
			ResolveInfo best2 = null;
			for (final ResolveInfo info : matches2)
				if (info.activityInfo.packageName.endsWith(".gm")
						|| info.activityInfo.name.toLowerCase().contains(
								"gmail"))
					best2 = info;
			if (best2 != null)
				intent2.setClassName(best2.activityInfo.packageName,
						best2.activityInfo.name);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	public static void Edit_List() {
		// TODO Auto-generated method stub
		int current_tab = mViewPager.getCurrentItem();
		switch (current_tab) {
		case 2:
			//SwipeyTabFragmentFavorites.Mode_Edit();
			edit_done.setVisibility(View.VISIBLE);
			edit_List.setVisibility(View.INVISIBLE);

			break;
		case 4:
			SwipeyTabFragmentMusicPlayList.Mode_Edit();
			edit_done.setVisibility(View.VISIBLE);
			edit_List.setVisibility(View.INVISIBLE);
			break;
		default:
			break;
		}
	}


	public void onBackPressed() {

		if (mViewPager.getCurrentItem() == 0) {
			this.finish();
			overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
		} else {
			mViewPager.setCurrentItem(0);
		}

		return;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent imageReturnedIntent) {
		super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
		Log.d("PlayList", " on Activity Result = "
				+ MainContainerActivity.PlaylistName);

		if (requestCode == 3) {
			try {
				PlaylistName = imageReturnedIntent.getType();
				ListMusic_Edit_PlayList();
				Log.d("PlayList", " on Activity Result = "
						+ MainContainerActivity.PlaylistName);
			} catch (Exception e) {
				// TODO: handle exception
			}

		}
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	public static void ListVideo_Edit() {
		// TODO Auto-generated method stub
		mViewPager.setCurrentItem(1);
		//SwipeyTabFragmentVideos.Mode_Edit_On();
		edit_done.setVisibility(View.VISIBLE);
		request.setVisibility(View.INVISIBLE);

	}

	public static void ListMusic_Edit_PlayList() {
		// TODO Auto-generated method stub
		mViewPager.setCurrentItem(3);
		SwipeyTabFragmentMusic.Mode_Edit_On();
		edit_done.setVisibility(View.VISIBLE);
		request.setVisibility(View.INVISIBLE);

	}

	/**************
	 * 
	 * Media Player
	 * 
	 **************/
	public static MediaPlayer mediaPlayer = new MediaPlayer();
	public static Boolean MediaPlayer_Connect = false;
	public static Boolean MediaPlayer_Playing = false;
	public static int MediaPlayer_Mode = 1;
	public static String MediaPlayer_Url = "";
	public static Boolean MediaPlayer_finish_seeking = false;

	public static void Stop_MediaPlayer() {
		MediaPlayer_Connect = false;
		try {
			mediaPlayer.stop();
		} catch (Exception e) {
			// TODO: handle exception
		}
		MediaPlayer_Playing = false;
	}

	public static void Next_MediaPlayer() {
		if (Mode_playlist_currentPosition == (Mode_playlist_List.size() - 1)) {
			Mode_playlist_currentPosition = 0;
		} else {
			Mode_playlist_currentPosition = Mode_playlist_currentPosition + 1;
		}
		Stop_MediaPlayer();
		MusicPlayer
				.Update_UI(Mode_playlist_List
						.get(Mode_playlist_currentPosition).getName(),
						Mode_playlist_List.get(Mode_playlist_currentPosition)
								.getFeatring());
		Lancer_MediaPlayer_mode_playlist(Mode_playlist_List,
				Mode_playlist_currentPosition);

	}

	static int Mode_playlist_currentPosition = 0;
	static ArrayList<Music> Mode_playlist_List = new ArrayList<Music>();

	public static void Lancer_MediaPlayer_mode_playlist(
			ArrayList<Music> ListMusic, int position) {

		Log.d("Sizep", " current " + position);
		Mode_playlist_List = ListMusic;
		Mode_playlist_currentPosition = position;
		MediaPlayer_Connect = true;
		MediaPlayer_Url = Mode_playlist_List.get(Mode_playlist_currentPosition)
				.getUrl();
		;
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(MediaPlayer_Url);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.prepareAsync();
			mediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {

				@Override
				public void onSeekComplete(MediaPlayer mp) {
					// TODO Auto-generated method stub
					MediaPlayer_finish_seeking = true;
				}
			});
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					if (MediaPlayer_Connect) {
						mediaPlayer.start();
						MediaPlayer_Playing = true;
					}
				}
			});

		} catch (Exception e) {
			// TODO: handle exception
			Log.d("Exception_Player", "error :  " + e.toString());
		}

	}

	public static void Lancer_MediaPlayer(String url, int Mode) {
		MediaPlayer_Mode = Mode;
		MediaPlayer_Connect = true;
		MediaPlayer_Url = url;
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(MediaPlayer_Url);
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

			mediaPlayer.prepareAsync();
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					if (MediaPlayer_Connect) {
						mediaPlayer.start();
						MediaPlayer_Playing = true;
					}
				}
			});

		} catch (Exception e) {
			// TODO: handle exception

		}

	}

	

}